
/**
 * 页面加载事件：
 */
function initData(){
    //加载供应商信息
    loadGYS();

    var json = JSON.parse(sessionStorage.getItem("billView"));
    document.getElementById("zdbh").value=json.zdbh;
    document.getElementById("spmc").value=json.spmc;
    document.getElementById("zdje").value=json.zdje;

    document.getElementById("gys").value=json.gys;

    var arr = document.getElementsByName("sffk");
    for(var i=0; i<arr.length; i++){
        if(arr[i].value == json.sffk){
            arr[i].setAttribute("checked","true");
        }else{
            arr[i].removeAttribute("checked");
        }
    }

    document.getElementById("zdbh").setAttribute("readonly","true");
    sessionStorage.removeItem("billView");
}   

//加载供应商信息
function loadGYS(){  
    var json = JSON.parse(sessionStorage.getItem("providerList"));
    var gys = document.getElementById("gys");
    for(var item of json){
        var opt = document.createElement("option");
        opt.innerText = item.gysmc;
        opt.value = item.gysmc;
        gys.appendChild(opt);
    }
}

//修改账单的保存按钮点击事件
function billSave(){
    //获取最新数据
    var zdbh = document.getElementById("zdbh").value;
    var spmc = document.getElementById("spmc").value;
    var gys = document.getElementById("gys").value;
    var zdje = document.getElementById("zdje").value;
    var arr = document.getElementsByName("sffk");
    var sffk = arr[0].checked ? arr[0].value : arr[1].value;
    //console.log(zdbh,spmc,gys,zdje,sffk);
    var json = JSON.parse(sessionStorage.getItem("billList"));
    for(var i=0; i<json.length; i++){
        if(json[i].zdbh == zdbh){
            json[i].spmc=spmc;
            json[i].gys=gys;
            json[i].zdje=zdje;
            json[i].sffk=sffk;
            break;
        }
    }
    sessionStorage.setItem("billList",JSON.stringify(json));
    //调用显示遮罩层
    addDiv("修改成功！");
}

//添加遮罩层提示
function addDiv(sm){
    var zhezhao = document.createElement("div");
    zhezhao.setAttribute("id","zhezhao");
    zhezhao.style.height="100%";
    zhezhao.style.width="100%";
    zhezhao.style.position="fixed";
    zhezhao.style.left="0px";
    zhezhao.style.top="0px";
    zhezhao.style.backgroundColor="black";
    zhezhao.style.opacity="0.5";

    var ts = document.createElement("div");
    ts.setAttribute("id","ts");
    ts.innerText=sm;
    ts.style.border="1px #00E4FF solid";
    ts.style.height="50px";
    ts.style.width="200px";
    ts.style.backgroundColor="#0070A4";
    ts.style.color="white";
    ts.style.textAlign="center";
    ts.style.lineHeight="50px";
    ts.style.position="fixed";
    ts.style.left="50%";
    ts.style.top="46%";
    ts.style.transform="translate(-50%,-50%)";
    ts.style.zIndex="1";
    document.body.appendChild(zhezhao);
    document.body.appendChild(ts);
    //清除提示
    clearTS();
}

//移除提示相关div
function removeDiv(sm){
    document.getElementById("zhezhao").remove();
    document.getElementById("ts").remove();
    location.href="billList.html";
}

//延迟1s移除的延迟器
function clearTS(){
    window.setTimeout("removeDiv()",1000);
} 